package com.tql.kingdee.service;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.datasource.annotation.Master;
import com.ruoyi.common.datasource.annotation.Slave;
import com.tql.kingdee.bean.TqlStapPriceBean;
import com.tql.kingdee.mapper.MaterialIncaseMapper;
import com.tql.kingdee.mapper.TqlMaterialPurchaseMapper;
import com.tql.kingdee.response.MaterialIncaseResponse;
import com.tql.kingdee.response.MaterialPurchaseResponse;
import com.tql.kingdee.util.CommonUtil;
import com.tql.kingdee.util.TimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MaterialPurchaseService {

    @Autowired
    private TqlMaterialPurchaseMapper materialPurchaseMapper;

    @Autowired
    private MaterialIncaseMapper materialIncaseMapper;

    @Autowired
    private DsTestService dsTestService;

@Master
   public void batchAddStapPrice(List<MaterialPurchaseResponse> list)
    {
        if(CollectionUtils.isEmpty(list))return;
        List<TqlStapPriceBean> batchInsert = new ArrayList<>();
        for (MaterialPurchaseResponse incase : list) {
//            TqlStapPriceBean tqlStapPrice = materialIncaseMapper.getTqlStapPrice(incase.getFMaterialIdfnumber(), incase.getFStockIdfnumber());
            TqlStapPriceBean tqlStapPrice = new TqlStapPriceBean();
            tqlStapPrice.setStapCode(incase.getFMaterialIdfnumber());
            tqlStapPrice.setStapName(incase.getFMaterialIdfname());
            tqlStapPrice.setLastPrice(incase.getFPrice());
            tqlStapPrice.setWeightedPrice(incase.getFPrice());
            if (StringUtils.isNotBlank(incase.getFStockIdfnumber()) && !incase.getFStockIdfnumber().startsWith("BU")){
                incase.setFStockIdfnumber(CommonUtil.deptCodeReplacePrefix(incase.getFStockIdfnumber()));
            }
            tqlStapPrice.setDeptCode(incase.getFStockIdfnumber());
            batchInsert.add(tqlStapPrice);
        }
        if (!batchInsert.isEmpty())
            materialIncaseMapper.insertBatchTqlStapPrice(batchInsert);

    }


    public void batchAddInchange(List<MaterialPurchaseResponse> list,int month)
    {
        if (!list.isEmpty()) {
            String inChangeTabName = CommonUtil.TQL_STAP_IN_CHANGE+month;
            dsTestService.batchAddPurchase(list,inChangeTabName, 2);
        }

    }



@Slave
    public void batchAdd(List<MaterialPurchaseResponse> list, int month) throws ParseException {

        String tabname= CommonUtil.TABLENAME_TQL_MATARIAL_PURCHASE+month;
        materialPurchaseMapper.batchAdd(list,tabname);


    }

    public List<MaterialPurchaseResponse> selectList(String tabname2) {
        return materialPurchaseMapper.selectList(tabname2);
    }
}
